<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
$return['post']=array();
$return['paytypes']=array(
		'credit'=>'余额支付',
		'wechat'=>'微信支付',
		'alipay'=>'支付宝支付',
		'yinlian'=>'银联支付',
		'hdfk'=>'货到付款',
);
if($_RQ['op']=='main'){
	exi($return);
}
/*******op*******/
set_html('title','配送订单');
if($_RQ['op']=='list'){
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status']!=100){
		if(intval($_RQ['status'])==1){
			$params['status']=array(1,6);
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['status']=$_RQ['status'];
	}elseif($_RQ['status']==100){
		$return['status']=100;
	}else{
		$params['status']=1;
		$return['status']=1;
	}
	if($_RQ['today']){
		$params['createtime >']=strtotime(date('ymd'));
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$order_list=pdo_getslice('jy_liangoods_sendlog',$params,array($pindex,$psize),$total,array(),'','createtime desc');
	if($order_list){
		foreach($order_list as &$item){
			$store=pdo_get('jy_weishop_store',array('id'=>$item['storeid'],'uniacid'=>$_SESSION['uniacid']));	
			$item['store']=$store;
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
            $order=pdo_get('jy_weishop_order',array('id'=>$item['order_id'],'uniacid'=>$_SESSION['uniacid']),array('is_peisong','paytype','ordersn','ispay','addressid'));
            if($order['is_peisong']==1){
                $item['sendtype']=4;
            }
            $item['sendtypetitle']=$this->send_types[$item['sendtype']];
			$item['ordersn']=$order['ordersn'];
			$item['paytype']=$order['paytype'];
			$item['ispay']=$order['ispay'];
			$item['statustitle']=$this->sendlog_status[$item['status']];
			//用户地址
			$address['pcd']=json_decode($item['pcd'],true);
			$address['name']=$item['name']?cfc('tools')->substr($item['name'],0,1).'****':'无姓名';
			$address['mobile']=$item['mobile']?cfc('tools')->substr($item['mobile'],0,7).'****':'无电话';
			$item['order_out']=$address;
			if($item['goods']){
				$item['goods']=json_decode($item['goods'],true);
			}
			$selfinfo=pdo_get('jy_weishop_order_self',array('orderid'=>$item['id']));
			$selfinfo['selftime']=$selfinfo['selftime']?date('Y-m-d H:i:s',$selfinfo['selftime']):'尽快配送';
			$selfinfo['selftime']=$selfinfo['out_time']?:$selfinfo['selftime'];
			$selfinfo['number2']=substr($selfinfo['number'],-4);
			$item['selfinfo']=$selfinfo;
			$item['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'total');
		}
	}
	$return['list']=$return['order_list']=$order_list;
	if($this->session('isopenvoice')){
		$return['isopenvoice']=$this->session('isopenvoice')!=2?1:0;	
	}else{
		$return['isopenvoice']=1;
	}
	exi($return);
}

//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_liangoods_sendlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	$user=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$store=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['storeid']));	
	$store_times=json_decode($store['serve_time'],true);
	$order['serve_time']=$store_times[$order['timeid']];
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && ($order['address']['name'] || $order['address']['realname'])){
		$address=$order['address'];
	}else{
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
	}
	if(!is_array($address['pcd'])){
		$address['pcd']=json_decode($address['pcd'],true);
	}
	$address['name']=$address['name']?:$address['realname'];
	$order['order_out']=$address;
	$order['paytime']=$order['paytime']?date('Y-m-d H:i:s', $order['paytime']):'';
	//自定义字段数据
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['store']=$store;
	exi($return);
}
if($_RQ['op']=='fa'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['sendtype']==1){
			if($post['express_type']==0){
				if($post['is_cainiao']){
					$jy_fahuo=plugin('jy_fahuo')->plugin_setting('basic');
					$nowkuai=$jy_fahuo['kuaidis'][$post['kuaikey']];
					$cpCode=$nowkuai['name']?:'SF';
					if(!$cpCode){
						exi('请选择快递公司！','error');
					}
					$express_id=$nowkuai['express_id'];
					$templateUrl=plugin('jy_fahuo')->model('model')->get_templateUrl($cpCode);
					$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['order_id']));
					$order['callDoorPickUp']=$post['isyue']?true:false;
					$order['doorPickUpTime']=$post['starttime'];
					$order['doorPickUpEndTime']=$post['endtime'];					
					$res=plugin('jy_fahuo')->model('api')->get_waybill($order,$cpCode,$templateUrl,$nowkuai);
					//print_r($res);exit;
					$cpCode=$res[0]['cpCode'];
					$expresssn=$res[0]['waybillCode'];
					if(!$expresssn){
						exi('获取面单发货失败！','error');
					}
					$data=array('express'=>$express_id,'expresssn'=>$expresssn);
					$this->model('merch')->order_send($id,$data,0);
					$data['cpcode']=$cpCode;
					$data['templateurl']=$templateUrl;
					$data['starttime']=$post['starttime'];
					$data['endtime']=$post['endtime'];
					plugin('jy_fahuo')->model('model')->uplog($order,$data);
				}else{
					if(!$post['express'] || !$post['expresssn']){
						exi('请填写快递信息','error');
					}
					$data=array('express'=>$post['express'],'expresssn'=>$post['expresssn']);
					$this->model('merch')->order_send($id,$data,0);
				}
			}elseif($post['express_type']==1){//平台配送
				$this->model('merch')->order_send($id,$data,1);			
				exi('订单配送处理成功！','','/merch.worker_order/list/status=1');
			}elseif($post['express_type']==2){//商家自己配送
				$this->model('merch')->order_send($id,'',1);
				exi('订单配送处理成功！','','/merch.worker_order/list/status=1');
			}elseif($post['express_type']==3){//达达配送
				$post['ordersn']=pdo_getcolumn('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['order_id']),'ordersn');
				$post['remark']=pdo_getcolumn('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['order_id']),'remark');
				$post['price']=pdo_getcolumn('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['order_id']),'price');
				$post['address']=array(
                        'address'=>$post['address'],
                        'pcd'=>$post['pcd'],
                        'mobile'=>$post['mobile'],
                        'name'=>$post['name'],
                 );
				$res=model('jy_weishop/order/dada_api')->dada_send($post,$merchid,1);
				if($res['state']==1){
					$data=array('opensend_type'=>1,'send_status'=>0);
					$this->model('merch')->order_send($id,$data,1);
				}else{
					exi($res['msg'],'error');
				}
			}			
		}elseif($post['sendtype']==3){
			$this->model('merch')->order_send($id,$post['content']);
		}elseif($post['sendtype']==2){
			$this->model('merch')->order_send($id,$post['verifycode']);
		}
		exi('已发货！','','/merch.worker_order/list/status=1');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_liangoods_sendlog',array('id'=>$id));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单发货',
	);
	if($item['sendtype']==1){
		$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
		$item['citypeisong_isopen']=$citypeisong['isopen'];
		$item['citypeisong_express_type']=$citypeisong['express_type']?:0;
		if($citypeisong['isopen']==1 || $item['isdish']){
			$item['express_type']=$citypeisong['express_type']?:1;
            $order=pdo_get('jy_weishop_order',array('id'=>$item['order_id'],'uniacid'=>$_SESSION['uniacid']),array('is_peisong','paytype','ordersn','ispay','address','addressid'));
			if(!$item['isdish'] && $order['is_peisong']==2){
				$item['express_type']=0;
			}
			if($item['express_type']==1){
				$tip='商品订单已打包好,由平台配送员配送！';
			}elseif($item['express_type']==2){
				$tip='商品订单已打包好,商家自己配送,无需配送员！';
			}elseif($item['express_type']==3){
				$tip='商品订单已打包好,发送给达达配送！(<red>【慎重操作】：确定要呼叫《达达配送员》过来拿货吗?</red>)';
			}elseif(!$item['express_type']){
				$tip='商品订单已打包好,快递发货！';
			}
			$tip .='</br>同时将《打印出货标》！请把出货标，贴到外包装！';
		}else{
			$item['express_type']=0;
			$tip='商品订单已打包好,快递发货！';
		}
		if(!$item['express_type']){
			$item['is_cainiao']=0;
			if($return['pluginset']['is_cainiao'] && $this->plugin_exists('jy_fahuo')){
				$tip.='[菜鸟LINK]获取面单！';
				$item['is_cainiao']=1;
				$jy_fahuo=plugin('jy_fahuo')->plugin_setting('basic');
				$return['kuaikeys']=$jy_fahuo['kuaidis'];
				foreach($return['kuaikeys'] as $k=>&$ev){
					$ev['id']=$k;
				}
				$item['kuaikey']='0';
			}
		}
		$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('express_type'=>0,'is_cainiao'=>0)),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('express_type'=>0,'is_cainiao'=>0)),
			array('type'=>'select','title'=>'菜鸟LINK快递公司','name'=>'kuaikey','options'=>'kuaikeys','show'=>array('express_type'=>0,'is_cainiao'=>1)),
			array('type'=>'switch','title'=>'是否预约上门','name'=>'isyue','show'=>array('express_type'=>0,'is_cainiao'=>1)),
			array('type'=>'time','title'=>'预约上门收件时间','name'=>'starttime','show'=>array('isyue'=>1,'express_type'=>0,'is_cainiao'=>1)),
			array('type'=>'time','title'=>'预约上门截止时间','name'=>'endtime','show'=>array('isyue'=>1,'express_type'=>0,'is_cainiao'=>1)),
		);
	}else{
		if($item['goods']){
			$item['goods']=json_decode($item['goods'],true);
		}
		$goods=$item['goods'][0];
		$item=array_merge($goods,$item);
		$htmls['edit']=array(
			array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
			array('type'=>'image','title'=>'商品图片','name'=>'image','read'=>true),
			array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2)),
			array('type'=>'text','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'=>3),'help'=>'虚拟物品发货，填写发货内容！'),
			
		);
	}
	$return['express']=pdo_getall('jy_weishop_express');
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
//员工确认订单
if($_RQ['op']=='receive'){
	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('merch')->order_receive($order_id);
	exi('恭喜你完成一单！感谢你的努力！');
}
//员工取消订单
if($_RQ['op']=='unreceive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_liangoods_sendlog',array('status'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('取消订单成功');
}
if($_RQ['op']=='upimage'){
	$postdata=get_postdata();
	$log_id=intval($postdata['log_id']);
	if(!$log_id){
		exi('订单不存在，请重新选择订单','error');
	}
	if(count($postdata['images'])<=1){
		exi('图片必须2张以上（条码部分+外包装的照片）！','error');
	}
	pdo_update('jy_liangoods_sendlog',array('status'=>2,'images'=>ijson_encode($postdata['images'])),array('uniacid'=>$_SESSION['uniacid'],'id'=>$log_id));
    $this->model('notice')->notice_checkorder($log_id);
	exi('上传成功！');
}
if($_RQ['op']=='getnew'){
    $params=array(
        'status'=>array(1,3),
        'uniacid'=>$_SESSION['uniacid'],
    );
    $this->session('isopenvoice',1);
    $order=pdo_get('jy_liangoods_sendlog',$params);
    if($order['id']){
        exi($order);
    }else{
        exi('none');
    }
}